<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\AdminUser;
use App\Models\ProjectUser;
use App\Models\Project;
use Hash;
use Log;
//20210524
class TemplateMessageController extends Controller
{
    //发送报警 模板消息 跳转小程序
    public function send($info){
        $users = DB::table('xfy_device')->where(['xfy_device.cid'=>$info['cid']])
            ->leftJoin('xfy_project', 'xfy_device.projectId', '=', 'xfy_project.projectId')->
            select('xfy_device.*')->get()->toArray();
        $user = Db::table('admin_users')->where(['id'=>$users[0]['user_id']])->orWhere('pid', '=', $users[0]['user_id'])->get()->toArray();
        $ids = array_column($user, 'unionid');
        $union = DB::table('users')->whereIn('unionid', $ids)->get()->toArray();
        $openid_list = array_column($union,'openid');
        $app = app('wechat.official_account');
        foreach ($openid_list as $key => $value) {
        $res = $app->template_message->send([
                'touser' => $info['user'],
                'template_id' => 'LIpTip4j-gTGA_yjFov3aUqqRc9ATNY0iTy11I4k0wU',
                'miniprogram' => [
                        'appid' => 'wx301bce58badb7cc3',
                        'pagepath' => 'pages/v2/Log/Log',
                ],
                'data' => [
                    "first" => [
                        "value"=>$info['info'], //报警内容
                        "color"=>"#173177"
                    ],
                    "keyword1" => [
                        "value"=>$info['projectname'], //项目名称
                        "color"=>"#173177"
                    ],
                    "keyword2"=> [
                        "value"=>$info['cid'], //设备ID
                        "color"=>"#173177"
                    ],
                    "keyword3"=> [
                        "value"=>$info['address'], //设备地址
                        "color"=>"#173177"
                    ],
                    "keyword4"=> [
                        "value"=>$time, //报警时间
                        "color"=>"#173177"
                    ],
                    "remark" => [
                        "value"=>'请尽快处理确定报警设备',
                        "color"=>"#173177"
                        ]
                ],
        ]);
        }
    }
}